package com.useeinfo.oa.modules.maintaining.entity;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.entity.SuperClassEntity;
import com.useeinfo.framework.sugar.tools.CommonSugar;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.base.entity.Resource;
import com.useeinfo.oa.modules.maintaining.enums.InputTypeEnum;
import com.useeinfo.oa.modules.security.entity.SystemUser;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.util.*;

/**
 * @program: YiYouFuPlatform
 * @author: Taogq
 * @createDate: 2019-01-04
 * @createTime: 09:32:59
 * 维保工单--设备与工艺对应的工作内容
 */
@Entity
@Table(name = "MaintainOrderWorkContent")
public class MaintainOrderWorkContent extends SuperClassEntity<MaintainOrderWorkContent> {

    @ManyToOne
    @JoinColumn(name = "facilityMaintainSnapId")
    private FacilityMaintainSnap facilityMaintainSnap;

    /**
     * 维保项目
     */
    private String name;
    /**
     * 维保类别（轻度保养 中度保养 深度保养）
     */
    private String category;
    /**
     * 应用场景ID
     */
    private Long application_scenarios_id;

    /**
     * 应用场景 名称
     */
    private String application_scenarios_name;

    /**
     * 输入类型
     */
    @Enumerated(value = EnumType.STRING)
    private InputTypeEnum inputTypeEnum;
    /**
     * 最大值
     */
    private Double maxNumber;
    /**
     * 最小值
     */
    private Double minNumber;
    /**
     * 数值单位
     */
    private String unit;
    /**
     * 是否需要拍照
     */
    @Column(columnDefinition = "bit default 0")
    private Boolean photo;
    /**
     * 维保内容
     */
    @Column(columnDefinition = "text")
    private String content;


    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "maintain_content_photo", joinColumns = {
            @JoinColumn(name = "maintainData_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "resource_id", referencedColumnName = "id")})
    private Set<Resource> contentPhoto = new HashSet<Resource>();


    /**
     * 上传pdf的id(单个)
     */
    private String resourceIds;

//    /**
//     * 附件文件ID(一个)
//     */
//    private String pdfId;


    /**
     * 时间戳
     */
    private Long timeStamp;


    /**
     * 内容完成时间
     */
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date finishTime;

    /**
     * 完成人员
     */
    @ManyToOne
    @JoinColumn(name = "systemUser_id")
    private SystemUser systemUser;

    /**
     * 填入值
     */
    private String result;

    /**
     * 是否异常
     */
    @Column(columnDefinition = "bit default 0")
    private Boolean abnormal;
    /**
     * 是否已分配给参与人
     */
    private boolean distribute = false;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public InputTypeEnum getInputTypeEnum() {
        return inputTypeEnum;
    }

    public void setInputTypeEnum(InputTypeEnum inputTypeEnum) {
        this.inputTypeEnum = inputTypeEnum;
    }

    public Double getMaxNumber() {
        return maxNumber;
    }

    public void setMaxNumber(Double maxNumber) {
        this.maxNumber = maxNumber;
    }

    public Double getMinNumber() {
        return minNumber;
    }

    public void setMinNumber(Double minNumber) {
        this.minNumber = minNumber;
    }

    public String getUnit() {
        return unit;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }

    public Boolean getPhoto() {
        return photo;
    }

    public void setPhoto(Boolean photo) {
        this.photo = photo;
    }

/*    public Set<Resource> getEnclosure() {
        return enclosure;
    }

    public void setEnclosure(Set<Resource> enclosure) {
        this.enclosure = enclosure;
    }*/

    public Long getTimeStamp() {
        return timeStamp;
    }

    public void setTimeStamp(Long timeStamp) {
        this.timeStamp = timeStamp;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public FacilityMaintainSnap getFacilityMaintainSnap() {
        return facilityMaintainSnap;
    }

    public void setFacilityMaintainSnap(FacilityMaintainSnap facilityMaintainSnap) {
        this.facilityMaintainSnap = facilityMaintainSnap;
    }

    public Long getApplication_scenarios_id() {
        return application_scenarios_id;
    }

    public void setApplication_scenarios_id(Long application_scenarios_id) {
        this.application_scenarios_id = application_scenarios_id;
    }

    public String getApplication_scenarios_name() {
        return application_scenarios_name;
    }

    public void setApplication_scenarios_name(String application_scenarios_name) {
        this.application_scenarios_name = application_scenarios_name;
    }

    public Date getFinishTime() {
        return finishTime;
    }

    public void setFinishTime(Date finishTime) {
        this.finishTime = finishTime;
    }

    public SystemUser getSystemUser() {
        return systemUser;
    }

    public void setSystemUser(SystemUser systemUser) {
        this.systemUser = systemUser;
    }

    public String getResourceIds() {
        return resourceIds;
    }

    public void setResourceIds(String resourceIds) {
        this.resourceIds = resourceIds;
    }

    public String getResult() {
        return result;
    }

    public void setResult(String result) {
        this.result = result;
    }

    public Boolean getAbnormal() {
        return abnormal;
    }

    public void setAbnormal(Boolean abnormal) {
        this.abnormal = abnormal;
    }

    public Set<Resource> getContentPhoto() {
        return contentPhoto;
    }

    public void setContentPhoto(Set<Resource> contentPhoto) {
        this.contentPhoto = contentPhoto;
    }

    public boolean isDistribute() {
        return distribute;
    }

    public void setDistribute(boolean distribute) {
        this.distribute = distribute;
    }

//    public String getPdfId() {
//        return pdfId;
//    }
//
//    public void setPdfId(String pdfId) {
//        this.pdfId = pdfId;
//    }

    @Override
    public JSONObject toJSONObject() {
        JSONObject jsonObject = super.toJSONObject();
        jsonObject.put("name", StringConverters.null2String(getName()));
        jsonObject.put("category", StringConverters.null2String(getCategory()));
        jsonObject.put("result", StringConverters.null2String(getResult()));
        if (getInputTypeEnum() != null) {
            jsonObject.put("inputTypeEnum", StringConverters.null2String(getInputTypeEnum().getName()));
            if(getInputTypeEnum().getCode().equals(InputTypeEnum.IS_NORMAL.getCode())){
                jsonObject.put("resultText", "true".equals(getResult())?"正常":"异常");
            }else if(getInputTypeEnum().getCode().equals(InputTypeEnum.SWITCH.getCode())){
                jsonObject.put("resultText", "true".equals(getResult())?"开":"关");
            }else{
                jsonObject.put("resultText", StringConverters.null2String(getResult()));
            }
        } else {
            jsonObject.put("inputTypeEnum", "");
            jsonObject.put("resultText", StringConverters.null2String(getResult()));
        }
        jsonObject.put("abnormal", StringConverters.null2Boolean(getAbnormal()));
        jsonObject.put("abnormalText", CommonSugar.getTypedDefault(getAbnormal(), false)?"异常":"正常");
        jsonObject.put("maxNumber", StringConverters.null2Double(getMaxNumber()));
        jsonObject.put("minNumber", StringConverters.null2Double(getMinNumber()));
        jsonObject.put("applicationName", StringConverters.null2String(getApplication_scenarios_name()));
        jsonObject.put("applicationId", StringConverters.null2String(getApplication_scenarios_id()));
        jsonObject.put("content", StringConverters.null2String(getContent()));
        jsonObject.put("unit", StringConverters.null2String(getUnit()));
        JSONArray photoArray = new JSONArray();
        if (getContentPhoto() != null) {
            List<Resource> contentPhoto = new ArrayList<>(getContentPhoto());
            contentPhoto.sort((o1, o2) -> o1.getId() < o2.getId() ? -1 : 1);
            if (CollectionUtils.isNotEmpty(contentPhoto)) {
                for (Resource resource : contentPhoto) {
                    photoArray.add(resource.getPath());
                }
            }
        }
        jsonObject.put("photos", photoArray);

        return jsonObject;
    }

}
